package br.com.dufs.mvc.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.security.core.GrantedAuthority;

import com.fscoreunipe.app.infra.annotations.Create_AT;

@Create_AT
@Entity
@Table(name = "PERFIL")
public class Perfil implements java.io.Serializable, GrantedAuthority {

		private static final long serialVersionUID = 1L;

		@Id
		@Column(name = "descricao", unique = true)
		private String descricao;

		@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
		@JoinTable(name = "usuario_perfil", joinColumns = @JoinColumn(name = "id_perfil"), inverseJoinColumns = @JoinColumn(name ="id_usuario"))
		private List<Usuario> usuarios = new ArrayList<Usuario>();

		@Transient
		public String getAuthority() {
			return this.descricao;
		}

		@Transient
		public int compareTo(Object o) {
			return this.compareTo(o);
		}

		public String getDescricao() {
			return descricao;
		}

		public void setDescricao(String descricao) {
			this.descricao = descricao;
		}

		public List<Usuario> getUsuarios() {
			return usuarios;
		}

		public void setUsuarios(List<Usuario> usuarios) {
			this.usuarios = usuarios;
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result
				+ ((descricao == null) ? 0 : descricao.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Perfil other = (Perfil) obj;
			if (descricao == null) {
				if (other.descricao != null)
					return false;
			} else if (!descricao.equals(other.descricao))
				return false;
			return true;
		}

		@Override
		public String toString() {
			return "Perfil [descricao=" + descricao + "]";
		}
}
